home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 6 / MacMania 6.toast / / Multimedia & Desktop / sk8 / SK8InJava / Code / Renderers / ImageRenderer.java < prev    next >
Encoding:
Java Source  |  1997-02-27  |  2.7 KB  |  106 lines  |  [TEXT/CWIE]

  1. /*  SK8 © 1997 Apple Computer, Inc.
  2.     This code is protected under the current SK8 License
  3.     See http://sk8.research.apple.com/ for more information
  4.     Apple Research Laboratories
  5. */
  6.  
  7.  
  8. import java.awt.*;
  9. import java.applet.Applet;
  10.  
  11.  
  12. class imagerenderer extends renderer {
  13.  
  14.     //PROPERTIES AND THEIR GETTERS AND SETTERS.
  15.  
  16.     private String filenameVar;
  17.     private Image currentImage;
  18.     private renderer backgroundrendererVar = sk8.white;
  19.     Applet currentApplet;
  20.  
  21.     public imagerenderer () {
  22.         this.currentImage = null;
  23.     }
  24.     
  25.     public imagerenderer (String s) {
  26.         this.setfilename(s);
  27.     }
  28.     
  29.     public imagerenderer (String s, Applet app) {
  30.         this.setfilename(s, app);
  31.     }
  32.     
  33.     public String filename() {
  34.         return filenameVar;
  35.     }
  36.     public void setfilename(String s) {
  37.         setfilename(s, sk8.currentApplet);
  38.     }
  39.     public void setfilename(String s, Applet app) {
  40.         filenameVar = s;
  41.         currentImage = app.getImage(app.getDocumentBase(), filenameVar);
  42.         currentApplet = app;
  43.     }
  44.  
  45.     public renderer backgroundrenderer() {
  46.         return backgroundrendererVar;
  47.     }
  48.     public void setbackgroundrenderer(renderer r) {
  49.         backgroundrendererVar = r;
  50.     }
  51.     
  52.     private Point absolutepositionVar;
  53.     public Point absoluteposition() {
  54.         return absolutepositionVar;
  55.     }
  56.     public void setabsoluteposition(Point r) {
  57.         absolutepositionVar = r;
  58.     }
  59.  
  60.     public int height() {
  61.             //because java deals with images async'ly, we'll just wait for the 
  62.             //image to finish loading, since SK8 deals with these guys sync'ly
  63.             int h = -1;
  64.             while (h == -1){
  65.                     h = currentImage.getHeight(sk8.currentApplet);
  66.             }
  67.             return h;
  68.     }       
  69.     
  70.     public int width() {
  71.             //because java deals with images async'ly, we'll just wait for the 
  72.             //image to finish loading, since SK8 deals with these guys sync'ly
  73.             int w = -1;
  74.             while (w == -1){
  75.                     w = currentImage.getWidth(sk8.currentApplet);
  76.             }
  77.             return w;
  78.     }       
  79.  
  80.  
  81.     //THE RENDER METHOD
  82.     void render(Graphics g, actor act, Region reg) {
  83.         Rectangle drawrect;
  84.         if (absolutepositionVar != null)
  85.             drawrect = new Rectangle(absolutepositionVar.x, absolutepositionVar.y, 0,0);
  86.         else
  87.             drawrect = act.boundsrect(true);
  88.         // Render a background renderer is one is specified for a non rectangular image
  89.         if (backgroundrendererVar != null)
  90.             backgroundrendererVar.render(g, act, reg);
  91.         // If a proper image is specified, render that
  92.         if ((currentImage != null) && (currentApplet != null))
  93.             reg.drawImageRegion(g,
  94.                                 currentImage, 
  95.                                 drawrect,
  96.                                 currentApplet);
  97.         // In the worst case when there is no image and no 
  98.         // backgroundRenderer, render white
  99.         else if (backgroundrendererVar == null) 
  100.                 sk8.white.render(g, act, reg);
  101.  
  102.     }    
  103.  
  104. }
  105.  
  106.